﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManagerSystem
{
    //------------------------------------------------
    //单例模式
    public class StudentsSystem : Singleton<StudentsSystem>
    {
        public InputManager InputManager;
        private List<Student> StudentsList;

        public StudentsSystem()
        {
            InputManager = new InputManager(this);
            StudentsList = new List<Student>();
        }

        /// <summary>
        /// 系统入口
        /// </summary>
        public void Start()
        {
            //画一个登录界面
            InputManager.LoginPanel();
            //InputManager.MainPanel();
        }

        /// <summary>
        /// 验证账号密码
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool CheckAccountPassword(string account, string password)
        {
            if (account == "111111" && password == "bb")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 添加学生
        /// </summary>
        /// <param name="s"></param>
        public void AddStudent(Student s)
        {
            if (IsHasStudent(s))
            {
                Student student = GetStudent(s.ID);
                student = s;
            }
            else
            {
                StudentsList.Add(s);
            }
        }

        /// <summary>
        /// 根据学号获取学生
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Student GetStudent(int id)
        {
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].ID == id)
                    return StudentsList[i];
            }
            return null;
        }

        /// <summary>
        /// 根据学号移除学生
        /// </summary>
        /// <param name="id"></param>
        public void RemoveStudent(int id)
        {
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].ID == id)
                {
                    StudentsList.RemoveAt(i);
                    break;
                }
            }
        }
        /// <summary>
        /// 获取指定年级的学生
        /// </summary>
        /// <param name="grade"></param>
        /// <returns></returns>
        public List<Student> GetStudents(E_GradeType grade)
        {
            List<Student> ret = new List<Student>();
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].Grade == grade)
                    ret.Add(StudentsList[i]);
            }
            return ret;
        }

        /// <summary>
        /// 获取指定年级所有成绩
        /// </summary>
        /// <param name="grade"></param>
        /// <returns></returns>
        public List<Score> GetScores(E_GradeType grade)
        {
            List<Score> ret = new List<Score>();
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].Grade == grade)
                    ret.Add(StudentsList[i].Score);
            }
            return ret;
        }
        /// <summary>
        /// 获取指定年级指定科目成绩
        /// </summary>
        /// <param name="grade"></param>
        /// <param name="course"></param>
        /// <returns></returns>
        public List<float> GetScores(E_GradeType grade, E_CourseType course)
        {
            List<float> ret = new List<float>();
            for (int i = 0; i < StudentsList.Count; i++)
            {
                ret.Add(StudentsList[i].Score.GetScore(course));
            }
            return ret;
        }
        /// <summary>
        /// 判断学生是否存在
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public bool IsHasStudent(Student s)
        {
            return IsHasStudent(s.ID);
        }
        /// <summary>
        /// 判断学生是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsHasStudent(int id)
        {
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].ID == id)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
